import { mockPrefix } from '@/config/settings'
import Mock from 'mockjs'

Mock.mock(mockPrefix + '/preload', 'post', (opt) => {
  const { body } = opt
  const b = JSON.parse(body)
  const { typebCode } = b
  let data: any = []
  if (typebCode == '11010101') {
    data = {
      activeTab: 'houseTab',
      tabs: [
        { name: 'houseTab', title: '产权信息(房地)' },
        { name: 'materialTab', title: '受理材料' }
      ],
      buttons: [
        { name: '保存', url: '/house/first/save' },
        { name: '前传', url: '/house/first/next' }
      ]
    }
  } else if (typebCode == '11010102') {
    data = {
      activeTab: 'houseTab',
      tabs: [
        { name: 'houseTab', title: '产权信息(房地)' },
        { name: 'materialTab', title: '受理材料' }
      ],
      urls: { nextUrl: '/house/change/next', saveUrl: '/house/change/save' }
    }
  } else {
    data = {
      tabs: [
        { name: 'areaTab', title: '产权信息(地)' },
        { name: 'materialTab', title: '受理材料' }
      ],
      urls: { nextUrl: '/area/first/next', saveUrl: '/area/first/save' }
    }
  }
  return {
    code: 200,
    data,
    msg: '预加载成功'
  }
})
Mock.Random.extend({
  phone: function () {
    const phonePrefix = ['132', '135', '189', '152', '139', '186']
    return this.pick(phonePrefix) + Mock.mock(/\d{8}/)
  }
})
const list = () =>
  Mock.mock({
    'tableList|3': [
      {
        name: '@cname',
        cardNo: '@id',
        cardType: '@pick(["1", "2",])',
        ownType: '@pick(["权利人", "义务人",])',
        type: '@pick(["共同共有", "单独共有",])',
        phone: '@phone',
        'edit|1': true
      }
    ],
    'materialList|1-10': [
      {
        name: '@cname',
        fileSource: '@pick(["纸质收取", "共享取得","电子证照",])',
        'number|1-2': 1,
        'selected|1': true
      }
    ]
  })

const mockCaseMessage = () =>
  Mock.mock({
    ywh: '@natural',
    date: '@date',
    sendDate: '@date',
    type: '国有建设用地使用权及房屋所有权/首次登记',
    workflowNode: '受理',
    netCode: '@natural'
  })

function mockHouseEdit() {
  return Mock.mock({
    'corporeInfoEdit|1': true,
    'dutyInfoEdit|1': true,
    'edit|1': true,
    'houseInfoEdit|1': true,
    'landInfoEdit|1': true,
    'landPurpListEdit|1': true,
    'ownershipAndPrintInfoEdit|1': true,
    'propOwnerListEdit|1': true,
    'registerReasonEdit|1': true,
    'transferInfoEdit|1': true
  })
}

function mockMaterialEdit() {
  return Mock.mock({
    'materialListEdit|1': true,
    'edit|1': true
  })
}

Mock.mock(mockPrefix + '/load', 'post', (opt) => {
  const { body } = opt
  const b = JSON.parse(body)
  const { typebCode } = b
  let data: any = []
  if (typebCode == '11010101') {
    data = {
      caseMessage: mockCaseMessage(),
      houseTab: { tableList: list().tableList },
      materialTab: { materialList: list().materialList },
      houseTabEdit: mockHouseEdit(),
      materialTabEdit: mockMaterialEdit()
    }
  } else if (typebCode == '11010102') {
    data = {
      caseMessage: mockCaseMessage(),
      houseTab: { tableList: list().tableList },
      materialTab: { tableList: list().materialList },
      houseTabEdit: mockHouseEdit(),
      materialTabEdit: mockMaterialEdit()
    }
  } else {
    data = {
      areaTab: { caseMessage: mockCaseMessage(), tableList: list().tableList },
      materialTab: { tableList: list().materialList },
      areaTabEdit: {},
      materialTabEdit: []
    }
  }
  return {
    code: 200,
    data,
    msg: '数据加载成功'
  }
})
